var map;
function loadScript() {
	var script = document.createElement("script");
	script.type = "text/javascript";
	keyS = GoogleKey;
	script.src = "http://maps.google.com/maps?file=api&v=2.x&key=" + keyS + "&async=2&callback=loadMap";
	document.body.appendChild(script);
}
 
function loadMap() {
	map = new GMap2(document.getElementById("theworld"));
	map.setMapType(G_PHYSICAL_MAP);
	//map.enableRotation();
	var cl = google.loader.ClientLocation;
	var cl_lat = cl.latitude;
	var cl_long = cl.longitude;
    var location = [cl.address.city, cl.address.region, cl.address.country].join(', ');        
    map.setCenter(new GLatLng(parseFloat(cl_lat),parseFloat(cl_long)), 5);
	document.getElementById('mylocation').innerHTML = "We believe you are located in: " + cl.address.country + ", which has as country code: " + cl.address.country_code + ".<br/><br/><h3>Details</h3>Longtitude = " + cl_long + ".<br/>Latitude = " + cl_lat + "<br/><br/>For your convenience we made it temporarely 'the centre of the universe'.";
	
	map.setUIToDefault();
	mapMe();
}
  
function mapMe() {
	var tb=document.getElementById("resultPix");
	var tr=tb.getElementsByTagName("tr");
map.clearOverlays();
	for (i=0; i<tr.length; i++){
		var td=tr[i].getElementsByTagName("td");		
		var sp=td[0].getElementsByTagName("span");
		var thumbref = td[6].getElementsByTagName("span");
		var titleref = td[3].getElementsByTagName("span");
		var authorref = td[8].getElementsByTagName("span");
		var docref = td[7].getElementsByTagName("span");
		var thumb = thumbref[0].firstChild.data;
		var title = titleref[0].firstChild.data;
		var author = authorref[0].firstChild.data;
		var doc = docref[0].firstChild.data;
		if (sp[0] != undefined)
		showAddress(sp[0].firstChild.data, map, title, author, thumb, doc);
	}
}
  
  function showAddress(address, m, title, author, thumb, doc) {
	  var geocoder = new GClientGeocoder();
	  geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	       // alert(address + " not found");
	      } else {
	        //map.setCenter(point, 13);
	        var marker = new GMarker(point);
	        m.addOverlay(marker);
	        
	        var info = "<table width=100%>"
	        info = info + "<tr><td colspan=2><h2>" + title + "</h2></td></tr>"
	        info = info + "<tr><td rowspan=\"2\" valign=\"top\" width=\"40\">" + thumb + "</td>"
	        info = info + "<td valign=top>" + "<p>by <span class=\"lotusPerson\">" + author + "</span></p></td></tr>"
	        info = info + "<tr><td valign=\"bottom\"><div align=\"right\">" + "<a href=\"./0/" + doc + "?OpenDocument\">Details..." + "</a></div></td></tr>"
	        info = info + "</table>" 
	  	  	var html = info;
		  	GEvent.addListener(marker, "click", function() {
		     marker.openInfoWindowHtml(html);
		  	})    
	        //marker.openInfoWindowHtml(address);
	      }
	    }
	  );
	}
	
	  function hideAddress(address, m) {
	  var geocoder = new GClientGeocoder();
	  geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	       // alert(address + " not found");
	      } else {
	        //map.setCenter(point, 13);
	        var marker = new GMarker(point);
	        m.removeOverlay(marker);
	        //marker.openInfoWindowHtml(address);
	      }
	    }
	  );
	}
	
  function showAddressInfo(address, m) {
	  var geocoder = new GClientGeocoder();
	  geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	       // alert(address + " not found");
	      } else {
	        //map.setCenter(point, 13);
	        var marker = new GMarker(point);
	        m.addOverlay(marker);
	  	  	var html = address;
		  	GEvent.addListener(marker, "click", function() {
		     marker.openInfoWindowHtml(html);
		  	})    
	        marker.openInfoWindowHtml(address);
	      }
	    }
	  );
	}


dojo.addOnLoad(loadScript);